package com.oliet.common.vo.dict;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.oliet.entity.Dictionary;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiaZhenpeng
 * @Title: DictionaryVO
 * @Package com.oliet.common.vo.dict
 * @Description: 字典
 * @date 2023-12-30 16:33
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MainDictionaryVO {

    private String mainCode;

    private String mainName;

    private List<ItemDictionaryVO> itemList;

    public static List<MainDictionaryVO> poListConverGetAllVoList(List<Dictionary> poList) {
        List<MainDictionaryVO> result = Lists.newArrayList();
        if (CollectionUtil.isEmpty(poList)) {
            return result;
        }
        Map<String, String> mainMap = poList.stream().collect(
                Collectors.toMap(
                        Dictionary::getMainCode,
                        Dictionary::getMainName,
                        (e1, e2) -> e1
                )
        );
        for (String mainCode : mainMap.keySet()) {
            // 过滤出当前主字典下的子字典
            List<ItemDictionaryVO> itemList = poList.stream()
                    .filter(po -> mainCode.equals(po.getMainCode()))
                    .map(po -> ItemDictionaryVO.builder()
                            .itemCode(po.getItemCode())
                            .itemName(po.getItemName())
                            .build()
                    ).collect(Collectors.toList());
            MainDictionaryVO main = MainDictionaryVO.builder()
                    .mainCode(mainCode)
                    .mainName(mainMap.get(mainCode))
                    .itemList(itemList)
                    .build();
            result.add(main);
        }
        return result;
    }
}
